#ifndef __THREAD__
#define __THREAD__

#include "../lib/define.h"
#include "../lib/handle.h"
#include "../lib/heap.h"
#include "../lib/rtl.h"

typedef struct PROCESS_CONTEXT {
  HANDLE_TYPE Signature;
  DWORD dwPrivilege;
  DWORD cr3;
  HANDLE hFirstThread;
  BOOLEAN bAdmin;
  BOOLEAN bKernel;
  WORD wReserved;
} * PPROCESS_CONTEXT, *LPPROCESS_CONTEXT;

typedef struct CPU_CONTEXT {
  DWORD edi;
  DWORD esi;
  DWORD ebp;
  DWORD esp;
  DWORD ebx;
  DWORD edx;
  DWORD ecx;
  DWORD eax;
  DWORD gs;
  DWORD fs;
  DWORD es;
  DWORD ds;
  DWORD err;
  DWORD eip;
  DWORD cs;
  DWORD eflags;
} * PCPU_CONTEXT, *LPCPU_CONTEXT;

typedef struct FPU_CONTEXT {
  TWORD st[10];
  WORD fcw;
  WORD fsw;
  WORD fflags;
  WORD feppv;
  QWORD ffwptr;
} * PFPU_CONTEXT, *LPFPU_CONTEXT;

typedef union XMMWORD {
  QWORD qword[2];
  DWORD dword[4];
  WORD word[8];
  BYTE byte[16];
} * PXMMWORD, *LPXMMWORD;

typedef struct THREAD_CONTEXT {
  HANDLE_TYPE Signature;
  struct CPU_CONTEXT cpu;
  struct FPU_CONTEXT fpu;
  union XMMWORD xmm[8];
  HANDLE hProcess;
  HANDLE hPrevThread;
  HANDLE hNextThread;
  DWORD dwCreateTime;
  DWORD dwNextStartTime;
  BOOLEAN bBlocked;
  BOOLEAN bSleep;
  WORD wReserved;
} * PTHREAD_CONTEXT, *LPTHREAD_CONTEXT;

extern HANDLE ThHandles[0x100];
extern DWORD ThCount;

void ThInitialize(void);
HANDLE ThCreateProcessThread(HANDLE hProcess, LPVOID lpEntry);
void ThSwitch(HANDLE hThread);

#endif
